<template>
  <div class="table-conent">
    <div class="header">
      <slot name="header">
        <div class="title">{{ title }}</div>
        <div class="handler">
          <slot name="headerHandler"></slot>
        </div>
      </slot>
    </div>
    <el-table
      class="table"
      :data="dataList"
      v-bind="childrenProps"
      style="width: 100%"
      border
      @selection-change="selectionChange"
      height="75vh"
    >
      <el-table-column
        v-if="showSelectColumn"
        type="selection"
        align="center"
        width="60"
      ></el-table-column>
      <el-table-column
        v-if="showIndexColumn"
        label="序号"
        type="index"
        width="50"
        align="center"
      />
      <template v-for="propItem in propsList" :key="propItem.prop">
        <el-table-column v-bind="propItem" align="center" show-overflow-tooltip>
          <template #default="scope">
            <slot :name="propItem.slotName" :row="scope.row">
              {{ scope.row[propItem.prop] }}
            </slot>
          </template>
        </el-table-column>
      </template>
    </el-table>
    <div class="footer" v-if="!showFooter">
      <slot name="footer">
        <el-pagination
          v-model="pageInfo.currentPage"
          :page-size="pageInfo.pageSize"
          :page-sizes="[10, 20, 30, 50]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalCount"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </slot>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, PropType, ref } from "vue";

export default defineComponent({
  emits: ["handleSelectionChange", "update:pageInfo"],
  props: {
    dataList: {
      type: Array as PropType<any[]>,
      default: true,
    },
    totalCount: {
      type: Number,
      default: () => 0,
    },
    propsList: {
      type: Array as PropType<any[]>,
      default: true,
    },
    showIndexColumn: {
      type: Boolean,
      default: () => false,
    },
    showSelectColumn: {
      type: Boolean,
      default: () => false,
    },
    title: {
      type: String,
      default: () => "列表",
    },
    // 分页设置
    pageInfo: {
      type: Object,
      default: () => ({
        pageSize: 10,
        currentPage: 1,
      }),
    },
    // 设置 tree
    childrenProps: {
      type: Object,
      defalut: () => ({}),
    },
    showFooter: {
      type: Boolean,
      defalut: () => true,
    },
  },
  setup(props, { emit }) {
    const selectionChange = (value: any) => {
      emit("handleSelectionChange", value);
    };
    const handleSizeChange = (pageSize: number) => {
      emit("update:pageInfo", { ...props.pageInfo, pageSize });
      // console.log(pageSize, "handleSizeChange");
    };
    const handleCurrentChange = (currentPage: number) => {
      emit("update:pageInfo", { ...props.pageInfo, currentPage });
      // console.log(currentPage);
    };
    return {
      selectionChange,
      handleSizeChange,
      handleCurrentChange,
    };
  },
});
</script>

<style lang="less" scoped>
.table-conent {
  padding: 15px;
  border-top: 10px;
  // background-color: lawngreen;
}
.header {
  display: flex;
  height: 45px;
  padding: 5px;
  justify-content: space-between;
  align-items: center;

  .title {
    font-size: 20px;
    font-weight: 700;
  }

  .handler {
    align-items: center;
  }
}

.footer {
  margin-top: 15px;
  .el-pagination {
    text-align: right;
  }
}
</style>
